/*Write a program that will read a line of text that ends with a period (note someone may enter more than one 
		sentence at a time), which serves as the sentinel value. Display all of the letters that occur in the 
		text, one per line and in alphabetical order, along with the number of times each letter occurs in the text.
		Use an array of base type int of length 26, so that the element at index 0 contains the number of a's, 
		the element at index 1 contains the number of b's, and so forth. Allow both uppercase and lowercase letters
		as input, but treat uppercase and lowercase versions of the same letter as being equal. You will need to 
		define a method that takes a character as an argument and returns an int value that is the correct index 
		for that character. For example, the argument 'a' would return the value 0, the argument 'b' would return 
		1. Allow the user to repeat this task until he or she wishes to stop.

Hints: Use one of the methods in the String class to convert the entire String to upper or lower case letters.
*/
/*
 * Author: Andrew Shore
 * Class: CSCE 145 Section 004
 * Homework 07 10-29-09
 * This program breaks up a sentence and displays the letters in alphabetical order and states how many times they
 * occur in the sentence.
 */
import java.util.Arrays;
import java.util.Scanner;
public class LetterCounter {
	
	//assigns a int value to each letter in the sentence that corresponds with its placement in the array
	public static int letterOrder(char aChar){
		int value=0;
		
		switch(aChar){
		case 'a': value= 0;   
		break;
		case 'b': value =1;
		break;
		case 'c': value =2;
		break;
		case 'd': value =3;
		break;
		case 'e': value =4;
		break;
		case 'f': value =5;
		break;
		case 'g': value =6;
		break;
		case 'h': value =7;
		break;
		case 'i': value =8;
		break;
		case 'j': value =9;
		break;
		case 'k': value=10;
		break;
		case 'l': value = 11;
		break;
		case 'm': value =12;
		break;
		case 'n': value = 13;
		break;
		case 'o': value =14;
		break;
		case 'p': value=15;
		break;
		case 'q': value = 16;
		break;
		case 'r': value =17;
		break;
		case 's': value =18;
		break;
		case 't': value =19;
		break;
		case 'u': value =20;
		break;
		case 'v': value =21;
		break;
		case 'w': value =22;
		break;
		case 'x': value =23;
		break;
		case 'y': value =24;
		break;
		case 'z': value =25;
		break;
		default:
		
		}
		
		return value;
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner keyboard=new Scanner(System.in);
		//creates new array that counts the number of times a letter occurs in the sentence.
		int[] numberOfLetters=new int[26];
		
		System.out.println("Welcome to the letter counter program.");
		
	boolean isContinue=true;
	//loops as long as user wants to enter a sentence
	while(isContinue){
		System.out.println("Please enter a setence to be analyzed.");
		
		String input=keyboard.nextLine();
		
		System.out.println(input);
		//takes the period out of the string
		String sentence=input.substring(0,input.indexOf('.'));
		
		//converts sentence to lower case to make it easier to work with
		String lowerCaseSentence=sentence.toLowerCase();
		
		//creates new array that puts letters in alphabetical order {uses letterOrder method later}
		char[] alphabeticalOrder=new char[26];
		
		//for loop that counts the number of times each letter occurs in a sentence using a switch statement
		for(int index=0; index<sentence.length(); index++){
		char a=lowerCaseSentence.charAt(index);
			switch(a){
			
			case 'a': numberOfLetters[0]=numberOfLetters[0]+1;
			break;
			case 'b': numberOfLetters[1]=numberOfLetters[1]+1;
			break;
			case 'c': numberOfLetters[2]=numberOfLetters[2]+1;
			break;
			case 'd': numberOfLetters[3]=numberOfLetters[3]+1;
			break;
			case 'e': numberOfLetters[4]=numberOfLetters[4]+1;
			break;
			case 'f': numberOfLetters[5]=numberOfLetters[5]+1;
			break;
			case 'g': numberOfLetters[6]=numberOfLetters[6]+1;
			break;
			case 'h': numberOfLetters[7]=numberOfLetters[7]+1;
			break;
			case 'i': numberOfLetters[8]=numberOfLetters[8]+1;
			break;
			case 'j': numberOfLetters[9]=numberOfLetters[9]+1;
			break;
			case 'k': numberOfLetters[10]=numberOfLetters[10]+1;
			break;
			case 'l': numberOfLetters[11]=numberOfLetters[11]+1;
			break;
			case 'm': numberOfLetters[12]=numberOfLetters[12]+1;
			break;
			case 'n': numberOfLetters[13]=numberOfLetters[13]+1;
			break;
			case 'o': numberOfLetters[14]=numberOfLetters[14]+1;
			break;
			case 'p': numberOfLetters[15]=numberOfLetters[15]+1;
			break;
			case 'q': numberOfLetters[16]=numberOfLetters[16]+1;
			break;
			case 'r': numberOfLetters[17]=numberOfLetters[17]+1;
			break;
			case 's': numberOfLetters[18]=numberOfLetters[18]+1;
			break;
			case 't': numberOfLetters[19]=numberOfLetters[19]+1;
			break;
			case 'u': numberOfLetters[20]=numberOfLetters[20]+1;
			break;
			case 'v': numberOfLetters[21]=numberOfLetters[21]+1;
			break;
			case 'w': numberOfLetters[22]=numberOfLetters[22]+1;
			break;
			case 'x': numberOfLetters[23]=numberOfLetters[23]+1;
			break;
			case 'y': numberOfLetters[24]=numberOfLetters[24]+1;
			break;
			case 'z': numberOfLetters[25]=numberOfLetters[25]+1;
			break;
			}
		}
		
		//sets all values in the array equal to 0 to allow me to avoid printing null values
		for(int index=0; index<26;index++){
			alphabeticalOrder[index]='0';
		}
		//uses the method defined above to sort letters
		for(int index=0; index<sentence.length();index++){
			
			
			alphabeticalOrder[letterOrder(lowerCaseSentence.charAt(index))]=lowerCaseSentence.charAt(index);
			
		}
		//prints all the letters in alphabetical order and displays how many times they occur
		for(int index=0; index<alphabeticalOrder.length;index++){
			if(alphabeticalOrder[index]=='0'){
				
			}
			else{
				char temp=alphabeticalOrder[index];
				int tempInt=numberOfLetters[index];
				System.out.println(temp+" "+tempInt);
				
			}
			
	}
		//resets the numberOfLetters array
		for(int index=0; index<26; index++){
			numberOfLetters[index]=0;
		}
		//asks user if they want to continue, if not then program terminates
		System.out.println("Do you want to continue (Yes/No)");
		String answer=keyboard.nextLine();
		if(answer.equalsIgnoreCase("no")){
			System.out.println("Thank you for using the program.");
			isContinue=false;
		}
		if(answer.equalsIgnoreCase("yes")){
			
		}
	}
}

}